<?php
    class Default_Model_User extends Zend_Db_Table_Abstract
    {
        protected $_name = 'user';
        protected $_primary = 'user_id';
        protected $_db;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function addUser($fullname, $email, $password, $gender)
        {
            $data = array(
                'email' => $email,
                'password' => md5($password),
                'avatar' => 0,
                'fullname' => $fullname,
                'gender' => $gender,
                'birthday' => '0000-00-00',
                'status' => 1,
                'level' => 3,
            );
            $result = $this->_db->insert($this->_name, $data);
        }
        
        public function checkUser($email)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('email = ?', $email);
            $result = $this->_db->fetchAll($sql);
            return count($result);
        }
        
        public function array_to_object($array = array()) {
            if (!empty($array)) {
                $data = false;
        
                foreach ($array as $akey => $aval) {
                    $data -> {$akey} = $aval;
                }
        
                return $data;
            }
        
            return false;
        }
        
        public function getName($id)
        {
            $facebookUrl = "https://graph.facebook.com/" . $id;
            $str = file_get_contents($facebookUrl);
            $result = json_decode($str);
            return $result->name;
        }
    }
